package com.onetime.mall.trading.controller;

import com.onetime.mall.trading.constants.ErrorMsg;
import com.onetime.mall.trading.entity.FavoriteEntity;
import com.onetime.mall.trading.service.FavoriteService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.R;
import com.onetime.mall.trading.vo.FavoriteProductsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 收藏信息表
 *
 * @author liyixuan
 * @email liyixuan106@163.com
 * @date 2022-03-26 19:38:29
 */
@RestController
@RequestMapping("favorite")
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;


    @PostMapping("/add")
    public R addFavorite(@RequestParam("userId")
                         @NotNull(message = "登录异常 请重新登录")
                         @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                         @RequestBody FavoriteEntity favoriteEntity) {
        favoriteEntity.setUserId(Long.valueOf(shUserId));
        favoriteEntity.setCreateTime(new Date());
        if (favoriteService.addFavorite(favoriteEntity)) {
            Long id = favoriteEntity.getId();
            return R.ok().put("data", id);
        }
        return R.error(ErrorMsg.FAVORITE_EXIT.getCode(), ErrorMsg.FAVORITE_EXIT.getMsg());
    }

    @GetMapping("/check")
    public R checkFavorite(@RequestParam("userId")
                           @NotNull(message = "登录异常 请重新登录")
                           @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                           @RequestParam Long idleId) {
        Long userId = Long.valueOf(shUserId);
        Integer favorite = favoriteService.isFavorite(userId, idleId);
        return R.ok().put("data", favorite);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = favoriteService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        FavoriteEntity favorite = favoriteService.getById(id);

        return R.ok().put("favorite", favorite);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FavoriteEntity favorite) {
        favoriteService.save(favorite);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FavoriteEntity favorite) {
        favoriteService.updateById(favorite);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        favoriteService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/delete")
    public R deleteFavorite(@RequestParam("userId")
                            @NotNull(message = "登录异常 请重新登录")
                            @NotEmpty(message = "登录异常 请重新登录") String shUserId,
                            @RequestParam Long id) {
        if (favoriteService.deleteFavorite(id)) {
            return R.ok();
        }
        return R.error(ErrorMsg.SYSTEM_ERROR.getCode(), ErrorMsg.SYSTEM_ERROR.getMsg());
    }

    @GetMapping("/my")
    public R getMyFavorite(@RequestParam("userId")
                           @NotNull(message = "登录异常 请重新登录")
                           @NotEmpty(message = "登录异常 请重新登录") String shUserId) {
        Long userId = Long.valueOf(shUserId);
        List<FavoriteProductsVo> list = favoriteService.getAllFavorite(userId);
        return R.ok().put("data", list);
    }

}
